package com.suray.wcs.service.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.commin.enmus.DeviceStatus;
import com.suray.wcs.service.system.entity.WareHouseDB;
import com.suray.wcs.service.system.mapper.WareHouseDBMapper;
import com.suray.wcs.service.system.service.IWareHouseDBService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (WareHouseDB)表服务实现类
 *
 * @author rongbj
 * @since 2020-02-18 13:05:30
 */
@Service("wcsDistrictService")
public class WareHouseDBServiceImpl extends ServiceImpl<WareHouseDBMapper, WareHouseDB> implements IWareHouseDBService {

    /**
     * 新增数据
     *
     * @param wareHouseDB 实例对象
     * @return 新增结果
     */
    @Override
    public boolean insertWcsDist(WareHouseDB wareHouseDB) {
        WareHouseDB wareHouseDBDTO = wareHouseDB;
        wareHouseDBDTO.setMapRotate(0);
        return this.save(wareHouseDBDTO);
    }

    /**
     * 修改数据
     *
     * @param wareHouseDB 实例对象
     * @return 修改结果
     */
    @Override
    public boolean updateWcsDist(WareHouseDB wareHouseDB) {
        WareHouseDB wareHouseDBDTO = wareHouseDB;
        wareHouseDBDTO.setMapRotate(0);
        boolean res;
        if (StringUtils.isBlank(wareHouseDBDTO.getWarehouseName())) {
            return false;
        }
        res = this.updateById(wareHouseDBDTO);
        return res;
    }

    /**
     * 根据库区名修改地图旋转角度
     *
     * @param districtName 库区名
     * @param mapRotate    地图旋转角度
     * @return 修改结果
     */
    @Override
    public boolean updateRotateByName(String districtName, Integer mapRotate) {
        QueryWrapper<WareHouseDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(WareHouseDB::getWarehouseName, districtName);
        WareHouseDB wareHouseDB = new WareHouseDB();
        wareHouseDB.setMapRotate(mapRotate);
        return this.update(wareHouseDB, queryWrapper);
    }

    /**
     * 查询库区配置是否可接收上位下发任务
     *
     * @param wareHouseDB 库区名
     * @return true 可接收上位下发的任务
     */
    @Override
    public boolean getIsAccept(WareHouseDB wareHouseDB) {
        // 判断依据，为1时，可接收，为其他时，不可接收
        if (wareHouseDB.getStatus().equals(DeviceStatus.DISABLE.getCode())) {
            return false;
        }
        return true;
    }

    /**
     * 查询库区配置是否同层多车
     *
     * @return true 可以同层多车
     */
    @Override
    public boolean getMoreForOne() {
        WareHouseDB curWareHouseDB = this.getCurWareHouseDB();
        if (curWareHouseDB != null) {
            // 默认取第一条数据做为配置项，判断依据，（0不可 1可）
            if (!curWareHouseDB.getMoreForOne()) {
                return false;
            }
        }
        return true;
    }


    /**
     * 分页查询
     *
     * @param wareHouseDB
     * @return
     */
    @Override
    public List<WareHouseDB> queryWareHouseList(WareHouseDB wareHouseDB) {
        QueryWrapper<WareHouseDB> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(wareHouseDB.getWarehouseName())) {
            queryWrapper.lambda().like(WareHouseDB::getWarehouseName, wareHouseDB.getWarehouseName());
        }
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public WareHouseDB queryWareHouseOne() {
        QueryWrapper<WareHouseDB> queryWrapper = new QueryWrapper<>();
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * @Author caox
     * @Description 查询所有的库区
     * @Date 2020/08/14
     **/
    @Override
    public List<WareHouseDB> queryAllWcsDist() {
        QueryWrapper<WareHouseDB> queryWrapper = new QueryWrapper<>();
        return this.list(queryWrapper);
    }

    /**
     * 按条件查询
     *
     * @param wareHouseName 查询条件仓库名称
     * @return 查询数据 结果集，如果是多个会抛出异常，随机取一条加上限制条件 wrapper.last("LIMIT 1")
     */
    @Override
    public WareHouseDB queryWareHouseByName(String wareHouseName) {
        QueryWrapper<WareHouseDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(WareHouseDB::getWarehouseName, wareHouseName);
        return this.getOne(queryWrapper);
    }
    /**
     * 按条件查询
     *
     * @param wareHouseCode 查询条件仓库编码
     * @return 查询数据 结果集，如果是多个会抛出异常，随机取一条加上限制条件 wrapper.last("LIMIT 1")
     */
    @Override
    public WareHouseDB queryWareHouseByCode(String wareHouseCode) {
        QueryWrapper<WareHouseDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(WareHouseDB::getWarehouseCode, wareHouseCode);
        return this.getOne(queryWrapper);
    }

    /**
     * 获取优先模式
     */
    @Override
    public Integer getPriorityMode() {
        WareHouseDB wareHouseDB = this.getCurWareHouseDB();
        if (wareHouseDB != null) {
            return wareHouseDB.getPriorityMode();
        }
        return null;
    }

    /**
     * 当前仓库对象
     *
     * @return
     */
    @Override
    public WareHouseDB getCurWareHouseDB() {
        QueryWrapper<WareHouseDB> queryWrapper = new QueryWrapper<>();
        WareHouseDB wareHouseDB = this.getOne(queryWrapper);
        return wareHouseDB;
    }


    @Override
    public int updatePortStatus(WareHouseDB wareHouseDB) {
        return baseMapper.updateById(wareHouseDB);
    }

    /**
     * 是否改变任务终点
     */
    @Override
    public boolean getIsChangeEnd() {
        WareHouseDB wareHouseDB = this.getCurWareHouseDB();
        if (wareHouseDB != null) {
            return wareHouseDB.getChangeEndFlag();
        }
        return false;
    }
}
